Page({
  data: {
    tempFilePaths:"",
    artcileContent:null,
    addImgFileId:null,
    artcileTitle:null,
    titel:"",
    artcileValue:""
    
  },
  chooseImage(){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success:res=>{
        const tempFilePaths = res.tempFilePaths
        this.setData({
          tempFilePaths:tempFilePaths[0]
        })
      }
    })
  },
  artcileContent(event){
    this.setData({
      artcileContent:event.detail.value
    })
  },
  async upImage(){
    let randomNum=Math.random()*100+Math.random()*100+Math.random()*100+Math.random()*100+Math.random()*100
    await wx.cloud.uploadFile({
      cloudPath: 'artcileImage/'+`${randomNum}`,
      filePath: this.data.tempFilePaths, // 文件路径
    }).then(res => {
      this.setData({
        addImgFileId:res.fileID
      })
    }).catch(error => {
        wx.showToast({
          title: '失败，请将信息填写完整',
          icon:"none"
      })
      console.log(error)
    }) 
  },
  setArtcileTitle(event){
    this.setData({
      artcileTitle:event.detail
    })
  },
  async pushAll(){
    const ui= wx.getStorageSync('userInfo');
    if(this.data.artcileContent && this.data.tempFilePaths  && ui.nickName && ui.openid && this.data.artcileTitle){
      await this.upImage();
      await wx.cloud.callFunction({
      name:"pushArtcile",
      data:{
        artcile:this.data.artcileContent,
        image:this.data.addImgFileId,
        nickName:ui.nickName,
        pushOpenid:ui.openid,
        artcileTitle:this.data.artcileTitle,
      }
    }).then(res=>{
      if(res.result){
      this.setData({
        titel:"",
        tempFilePaths:null,
        addImgFileId:null,
        artcileTitle:null,
        artcileContent:null,
        artcileValue:""
      })
      wx.showToast({
        title: '成功',
        icon:"success"
      })
    }else{
      wx.showToast({
        title: '含有违规内容请修改',
        icon:"none"
      })
      }
    }).catch(err=>{
      wx.showToast({
        title: '失败',
        icon:"none"
      })
    })  
  }else{
    wx.showToast({
      title: '失败',
      icon:"none"
    })
  }
  },
  onPullDownRefresh(){
    wx.stopPullDownRefresh();
  }
})